package com.azt.provider.impl;

import com.azt.api.pojo.Brand;
import com.azt.api.pojo.CategoryBrand;
import com.azt.api.service.ProCategoryBrandService;
import com.azt.model.page.Pagination;
import com.azt.provider.mapper.BrandMapper;
import com.azt.provider.mapper.CategoryBrandMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ProCategoryBrandServiceImpl implements ProCategoryBrandService {

	@Autowired
	private CategoryBrandMapper categoryBrandMapper;

	@Autowired
	private BrandMapper brandMapper;

	@Override
	public Pagination getProCategoryBrandPage(int pageno, int pagesize, Map<String, String> searchMap) {
		Example example = new Example(CategoryBrand.class);
		Example.Criteria criteria = example.createCriteria();
		String pid = searchMap.get("so_pid");
		if (!pid.isEmpty()) {
			criteria.andEqualTo("categoryTId", pid);
		}
		example.orderBy("seq").asc();

		Page<Object> startPage = PageHelper.startPage(pageno, pagesize);
		List<CategoryBrand> list = categoryBrandMapper.selectByExample(example);

		for (CategoryBrand categoryBrand : list) {
			if (categoryBrand.getBrandId() != null) {
				categoryBrand.setBrand(brandMapper.selectByPrimaryKey(categoryBrand.getBrandId()));
			}
		}

		Pagination p = new Pagination(startPage, list);
		p.setList(list);
		p.setParams(searchMap);
		return p;
	}

	@Override
	public CategoryBrand getEntityById(Integer id) {
		return categoryBrandMapper.selectByPrimaryKey(id);
	}

	@Override
	public void saveOrUpdate(CategoryBrand pcb) {
		if (pcb != null) {
			if (pcb.getId() == null) {
				categoryBrandMapper.insert(pcb);
			} else {
				categoryBrandMapper.updateByPrimaryKey(pcb);
			}
		}
	}

	@Override
	public void delete(CategoryBrand pcb) {
		categoryBrandMapper.deleteByPrimaryKey(pcb.getId());
	}

	@Override
	public void deleteMore(String[] ids) {
		for (String id : ids) {
			categoryBrandMapper.deleteByPrimaryKey(id);
		}
	}

	@Override
	public Boolean ValidateProCategoryBrands(Integer categoryTId, Integer brandId) {
		CategoryBrand cate = new CategoryBrand();
		cate.setBrandId(brandId);
		cate.setCategoryTId(categoryTId);
		List<CategoryBrand> cateb = categoryBrandMapper.select(cate);
		if (cateb != null && cateb.size()>0) {
			return false;
		} else {
			return true;// 没有返回true
		}
	}
	
	public List<CategoryBrand> ProCategoryBrandsByCategoryId(Integer categoryId,Integer cateType){
		CategoryBrand cate = new CategoryBrand();
		if(cateType==1){
			cate.setCategoryFId(categoryId);
		}
		if(cateType==2){
			cate.setCategorySId(categoryId);
		}
		if(cateType==3){
			cate.setCategoryTId(categoryId);
		}
		return categoryBrandMapper.select(cate);
	}

	@Override
	public List<CategoryBrand> getEntityByBrandId(Integer brandId) {
		CategoryBrand ca = new CategoryBrand();
		ca.setBrandId(brandId);
		return categoryBrandMapper.select(ca);
	}

	@Override
	public void CategoryBrandHandle(Brand probrand, List<Map> parseArray) {
		if (probrand.getId() != null) {
			List<CategoryBrand> catebs = this.getEntityByBrandId(probrand.getId());
			if (catebs != null && catebs.size() > 0) {
				for (CategoryBrand catb : catebs) {
					this.delete(catb);
				}
			}
		}

		for (Map<String, Object> mapa : parseArray) {
			CategoryBrand cab = new CategoryBrand();
			cab.setBrandId(probrand.getId());
			for (Map.Entry<String, Object> entry : mapa.entrySet()) {
				if ("categoryFId".equals(entry.getKey())) {
					cab.setCategoryFId((Integer) entry.getValue());
				}
				if ("categorySId".equals(entry.getKey())) {
					cab.setCategorySId((Integer) entry.getValue());
				}
				if ("categoryTId".equals(entry.getKey())) {
					cab.setCategoryTId((Integer) entry.getValue());
				}
			}
			this.saveOrUpdate(cab);
		}
	}

	@Override
	public List<Brand> findBrandByCategoryId(Integer state, Integer ifSelf, Integer categoryFId, Integer categorySId, Integer categoryTId) {
		return categoryBrandMapper.findBrandByCategoryId(state, ifSelf, categoryFId, categorySId, categoryTId);
	}
}
